﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QLNS_Client
{
    public partial class Frm_Them_TDPT : Form
    {
        string temp = "";
        string flag = "";
        ServiceReference1.Service_QLNSClient sv = new ServiceReference1.Service_QLNSClient();
        public Frm_Them_TDPT()
        {
            InitializeComponent();
        }

        private void Frm_Them_TDPT_Load(object sender, EventArgs e)
        {
            Load_TDPT();
        }
        public void Load_TDPT()
        {
            datagrid_TDPT.DataSource = sv.getTDPT();
            btn_Luu.Enabled = false;
            btn_Huy.Enabled = false;
        }

        private void datagrid_TDPT_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            txt_MaTDPT.Text = datagrid_TDPT.CurrentRow.Cells[0].Value.ToString();
            txt_TenTDPT.Text = datagrid_TDPT.CurrentRow.Cells[1].Value.ToString();
            temp = datagrid_TDPT.CurrentRow.Cells[0].Value.ToString();
            txt_MaTDPT.Enabled = false;
            txt_TenTDPT.Enabled = false;
        }

        private void btn_Them_Click(object sender, EventArgs e)
        {
            flag = "them";
            btn_Huy.Enabled = true;
            btn_Luu.Enabled = true;
            btn_Sua.Enabled = false;
            btn_Them.Enabled = true;
            btn_Thoat.Enabled = true;
            btn_Xoa.Enabled = false;
            txt_MaTDPT.Enabled = true;
            txt_TenTDPT.Enabled = true;
            txt_MaTDPT.Text = "";
            txt_TenTDPT.Text = "";
            txt_MaTDPT.Focus();
        }

        private void btn_Luu_Click(object sender, EventArgs e)
        {
            string a = txt_MaTDPT.Text;
            string b = txt_TenTDPT.Text;
            ServiceReference1.TD_PHOTHONG tdpt = new ServiceReference1.TD_PHOTHONG();
            tdpt.Ma_TDPT = a;
            tdpt.TenTDPT = b;
            if (a == "" | b == "")
            {
                MessageBox.Show("Xin nhập đầy đủ dữ liệu!");
            }
            else
            {
                int kq = 0;
                if (flag == "them")
                {
                    if (sv.check_tdpt(tdpt))
                        kq = -1;
                    else
                        kq = sv.addTDPT(tdpt);
                }
                else
                {
                    if (flag == "sua")
                    {
                        sv.setTDPT(tdpt);
                        kq = 1;
                    }
                }
                if (kq != -1)
                {
                    MessageBox.Show("Hoàn tất!");
                    Load_TDPT();
                    btn_Sua.Enabled = true;
                    btn_Xoa.Enabled = true;

                }
                else
                {
                    MessageBox.Show("Lỗi!!!");
                }
            }
        }

        private void btn_Huy_Click(object sender, EventArgs e)
        {
            btn_Sua.Enabled = true;
            btn_Xoa.Enabled = true;
            btn_Huy.Enabled = false;
            btn_Luu.Enabled = false;
        }

        private void btn_Xoa_Click(object sender, EventArgs e)
        {
            ServiceReference1.TD_PHOTHONG tdpt = new ServiceReference1.TD_PHOTHONG();
            tdpt.Ma_TDPT = temp;
            DialogResult dr = MessageBox.Show("Bạn thật sự muốn xóa dữ liệu này?", "warnring", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (dr == DialogResult.OK)
            {
                sv.deleteTDPT(tdpt);
                Load_TDPT();
                MessageBox.Show("Đã xóa!");
                txt_MaTDPT.Text = "";
                txt_TenTDPT.Text = "";
            }
        }

        private void btn_Sua_Click(object sender, EventArgs e)
        {
            flag = "sua";
            txt_MaTDPT.Enabled = false;
            txt_TenTDPT.Enabled = true;
            btn_Huy.Enabled = true;
            btn_Luu.Enabled = true;
        }

        private void btn_Thoat_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("Bạn thật sự muốn thoát bảng này?", "warnring", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (dr == DialogResult.OK)
            {
                this.Close();
            }
        }
    }
}
